package com.scau.os.course_manager;

import com.scau.os.MemorySys.Memory;
import com.scau.os.MemorySys.UseMemory;
import com.scau.os.course_manager.bean.PCB;
import com.scau.os.extension.EventSystem;

import java.util.ArrayList;
import java.util.List;

public class MemoryManager {
    public static final Memory MEMORY=new Memory(512);
    private UseMemory useMemory=new UseMemory();
    private List<List<Byte>> l=new ArrayList<>();
    /**
     * 已经维护好大小的PCB
     * @param pcb
     * @return
     */
    public boolean apply(PCB pcb){
        return useMemory.FF(MEMORY,pcb.size,pcb);
    }

    /**
     * 需要传入值的PCB
     * @param pcb
     * @param size
     * @return
     */
    public boolean apply(PCB pcb, int size,List<Byte> instructions){
        Boolean flag=useMemory.FF(MEMORY,size,pcb);
        if(!flag){
            l.add(instructions);
            System.out.println("等待设备分配");
        }
        return flag;
    }
    public boolean release(PCB pcb){
        boolean flag= useMemory.release(MEMORY,pcb);
        if(l.size()!=0){
            EventSystem.instance.SendEvent("executeStart",l.get(0));
            l.remove(0);
        }
        return flag;
    }
}
